package com.bite.springbootmybatis.mapper;

import com.bite.springbootmybatis.model.ArticleInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface ArticleInfoMapper {

    @Select("select ai.*, ui.username, ui.age from articleinfo ai " +
            "right join userinfo ui on ui.id = ai.uid " +
            "where ai.id = 1")
    ArticleInfo queryArticleInfo(Integer id);

    // #{} 与 ${} 区别
    // 1. #{} 是展位的 会自动添加 '' ${} 是即时占位 字符串拼接 所以如果是字符串需要添加 ''
    @Select("select * from articleinfo where id = #{id}")
    ArticleInfo queryArticleById(Integer id);

    @Select("select * from articleinfo where id = ${id}")
    ArticleInfo queryArticleById2(Integer id);

    // 2. ${} 存在 sql 注入的问题
    @Select("select * from articleinfo where title = '${title}'")
    List<ArticleInfo> queryArticleByTitle(String title);

    // 3. ${} 应用： 选择升序/降序 根据表名（大数据经行分表）查找...
    @Select("select * from articleinfo order by id ${order}")
    List<ArticleInfo> queryArticleById3(String order);

    // 不能用 ${} 因为 java 无法过滤内容
    @Select("select * from articleinfo where content like concat('%', #{like}, '%')")
    List<ArticleInfo> queryArticleLike(String like);
}
